<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>

    <meta charset="utf-8">

    <title>权限管理模块</title>
    <!--引入相关的js文件-->
    <link href="../backPage/ui/bootstrap-3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/waves-0.7.5/waves.css" rel="stylesheet"/>
    <link href="../backPage/ui/jquery-confirm/jquery-confirm.min.css" rel="stylesheet"/>
    <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>

    <script src="../backPage/ui/js/jquery-3.3.1.min.js"></script>
    <script src="../backPage/ui/waves-0.7.5/waves.js"></script>
    <script src="../backPage/ui/jquery-confirm/jquery-confirm.min.js"></script>
    <script src="../backPage/ui/bootstrap-3.3.0/js/bootstrap.min.js"></script>
    <script src="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.js"></script>
</head>

<body>
<div id="main">
    <div id="toolbar">
        <button class="btn btn-default" href="javascript:;" onclick="createAction()">新增权限信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="updateAction()">编辑权限信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="deleteAction()">删除权限信息</button>
        <br/>
    </div>
    <table id="table"></table>
</div>

<script>
    // 初始化input特效
    function initMaterialInput() {
        $('form input[type="text"]').each(function () {
            if ($(this).val() != '') {
                $(this).parent().find('label').addClass('active');
            }
        });
    }

    var $table = $('#table');

    //校验手机号码是否合法
    function validateTel(tel){
        var mobilereg1 = /^1[3|5|6|7|8|9][0-9]{9}$/; // 1[356789][0-9] （后面再接8位数）
        var mobilereg2 = /^14[5|7][0-9]{8}$/; // 14[57] （后面再接8位数）
        if (!mobilereg1.test(tel) && !mobilereg2.test(tel)) {
            return false;
        }
        return true;
    }

    function validateBirthday(birthday){
        var birthdayRegex = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/ig
        if(!birthdayRegex.test(birthday)){
            return false;
        }else{
            return true;
        }
    }

    $(function() {
        init();
        //新增权限信息
        $("#submitbutton").click(function() {
            var usertype = $("#usertype").val();
            var menuid = $("#menuid").val();

            $.ajax({
                url:"/studentManageSystem/addAuthorityInfo",
                type:"post",
                data:{usertype:usertype,menuid:menuid},
                success:function(data){
                    if(data == "OK"){
                        alert("添加成功");
                        $table.bootstrapTable('refresh');
                    }else if(data == ''){
                        alert("添加失败");
                    }else{
                        alert(data);
                    }
                }
            })

        });

        //更新学生信息
        $("#updateSubmitButton").click(function(){
            var authorityid = $("#authorityId").val();
            var usertype = $("#updateUsertype option:selected").val();
            var menuid = $("#updateMenuId option:selected").val();

            $.ajax({
                url:"/studentManageSystem/updateAuthorityInfo",
                type:"post",
                data:{authorityid:authorityid,usertype:usertype,menuid:menuid},
                success:function(data){
                    if(data=="OK"){
                        alert("修改成功");
                        $table.bootstrapTable('refresh');
                        $("#updateModal").modal("hide");
                    }else if(data == ''){
                        alert("修改失败");
                    }else{
                        alert(data);
                    }
                }
            })
        });


        function init(){
            $("#menuid").empty();
            $("#updateMenuId").empty();
            $.ajax({
                url:"/util/getMenuInfos",
                    type:"post",
                    success:function(data){
                        for(var i = 0;i<data.length;i++){
                            $("#menuid").append("<option value="+data[i]['menuid']+">"+data[i]['menuname']+"</option>")
                            $("#updateMenuId").append("<option value="+data[i]['menuid']+">"+data[i]['menuname']+"</option>");
                        }
                    }
            });

        }

        // bootstrap table初始化
        $table.bootstrapTable({
            url: '/studentManageSystem/getAuthorityinfos',
            height: getHeight(),
            striped: true,
            search: true,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns: 2,
            clickToSelect: true,
            detailView: true,
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            silentSort: false,
            sortOrder: "desc",
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'authorityid',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'authorityid', title: '权限编号', align: 'center'},
                {field: 'usertype', title: '用户角色编号',align: 'center'},
                {field: 'usertypename', title: '用户角色',align: 'center'},
                {field: 'menuid', title: '菜单编号',align: 'center'},
                {field: 'menuName', title: '菜单名称',align: 'center'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
            ]
        });
    });

    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        return [
            '<a class="update" href="javascript:;" onclick="updateAction()" data-toggle="tooltip" title="编辑"><i class="glyphicon glyphicon-edit"></i></a>　',
            '<a class="delete" href="javascript:;" onclick="deleteAction()" data-toggle="tooltip" title="删除"><i class="glyphicon glyphicon-remove"></i></a>'
        ].join('');
    }
    // 格式化时间
    function timeFormatter(value , row, index) {
        return new Date(value).toLocaleString().split(' ')[0];
    }

    function getHeight() {
        return $(window).height() - 20;
    }

    // 删除
    var deleteDialog;
    function deleteAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            deleteDialog = $.confirm({
                type: 'red',
                animationSpeed: 300,
                title: false,
                content: '确认删除该权限信息吗？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'waves-effect waves-button',
                        action: function () {
                            var ids = new Array();
                            for (var i in rows) {
                                ids.push(rows[i].authorityid);
                            }
                            $.ajax({
                                type: 'get',
                                url: '/studentManageSystem/deleteAuthorityInfoById/'+ids.join("-"),
                                success: function(result) {
                                    if(result == 'OK'){
                                        deleteDialog.close();
                                        $table.bootstrapTable('refresh');
                                        alert("删除成功");
                                    }else if(result == ''){
                                        alert("删除失败");
                                    }else{
                                        alert('管理人员不能删除权限管理模块，请重试');
                                    }
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    $.confirm({
                                        theme: 'dark',
                                        animation: 'rotateX',
                                        closeAnimation: 'rotateX',
                                        title: false,
                                        content: textStatus,
                                        buttons: {
                                            confirm: {
                                                text: '确认',
                                                btnClass: 'waves-effect waves-button waves-light'
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    },
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        }
    }

    //创建学生信息
    function createAction(){
        $("#createModal").modal("show");
    }

    //编辑学生信息
    function updateAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            //赋初值
            var obj = rows[0];
            $("#authorityId").val(obj['authorityid']);
            $("#updateUsertype option[value='"+obj['usertype']+"']").attr("selected","selected");
            $("#updateMenuId option[value='"+obj['menuid']+"']").attr("selected","selected");
            $("#updateModal").modal('show');
        }
    }

    //根据excel上传学生信息
    function uploadAction(){
        $("#uploadModal").modal("show");
    }
</script>

<!-- 添加权限信息模态框（Modal） -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="createModalTitle">
                    添加权限信息
                </h4>
            </div>
            <div class="modal-body">
                <form>

                    <div class="form-group">
                        <label for="usertype">用户类型</label>
                        <select class="form-control" id = "usertype" name="usertype">
                            <option value="1">管理员</option>
                            <option value="2">教师</option>
                            <option value="3">班干部</option>
                            <option value="4">学生</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="menuid"></label>
                        <select class="form-control" id = "menuid" name="menuid"></select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="submitbutton">
                    添加
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<!-- 修改权限信息模态框（Modal） -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="updateModalTitle">
                    编辑权限信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="authorityId"></label>
                        <input class="form-control" id="authorityId" name="authorityId" readonly="readonly"/>
                    </div>
                    <div class="form-group">
                        <label for="updateUsertype">用户角色</label>
                        <select class="form-control" id="updateUsertype" name="updateUsertype">
                            <option value="1">管理员</option>
                            <option value="2">教师</option>
                            <option value="3">班干部</option>
                            <option value="4">学生</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="updateMenuId">权限名称</label>
                        <select class="form-control" id="updateMenuId" name="updateMenuId"></select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="updateSubmitButton">
                    确认修改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

</body>
</html>